package cn.stronglink.oil.map;

import android.app.Activity;
import android.app.AlarmManager;
import android.bluetooth.BluetoothClass;
import android.support.v7.widget.CardView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TableLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.stronglink.oil.R;
import cn.stronglink.oil.entity.DeviceAlarmInfo;
import cn.stronglink.oil.entity.DeviceRealTimeInfo;
import cn.stronglink.oil.entity.GasHistoryInfo;
import cn.stronglink.oil.entity.GasType;
import cn.stronglink.oil.mq.DeviceGasDataManager;
import cn.stronglink.oil.widget.ExceptionHintDialog;
import cn.stronglink.oil.widget.ProgressDialog;
import me.codeboy.android.aligntextview.AlignTextView;

/**
 * Created by chen on 2017/5/16.
 */

public class DeviceInfoView implements AdapterView.OnItemClickListener {
    private Activity activity;
    private CardView cv_device_info;
    private TextView tv_fengxiang, tv_fengsu, tv_wendu, tv_shidu, tv_qiya, tv_pm25, tv_zaoyin, tv_gas_one_value,
           tv_gas_two_value, tv_gas_three_value, tv_gas_four_value, tv_gas_five_value,tv_gas_six_value, tv_gas_seven_value;
    private TextView tv_device_id;
    AlignTextView tv_gas_one_name, tv_gas_two_name, tv_gas_three_name, tv_gas_four_name,  tv_gas_five_name,
            tv_gas_six_name, tv_gas_seven_name;
    private ImageView iv_wind_pointer;
    private float currentDegree = 0;
    private ListView lv_history;
    private HistoryInfoAdapter historyInfoAdapter;
    private ProgressDialog progressDialog;
    private RadioButton rb_gas_type, rb_device_number;
    private TextView tv_filter_line;
    private ListView lv_filter_device_id, lv_filter_gas_type;
    private TableLayout tl_history_top;
    private FilterDeviceIdAdapter filterDeviceIdAdapter;
    private FilterGasTypeAdapter filterGasTypeAdapter;
    private String selectedDeviceId = null;
    private int selectedGasType = -1;
    private int selectDeviceNumber = -1, selectGasType = -1;

    public DeviceInfoView(Activity activity) {
        this.activity = activity;
        cv_device_info = (CardView) activity.findViewById(R.id.cv_device_info);
        tv_fengxiang = (TextView) activity.findViewById(R.id.tv_fengxiang);
        tv_fengsu = (TextView) activity.findViewById(R.id.tv_fengsu);
        tv_wendu = (TextView) activity.findViewById(R.id.tv_wendu);
        tv_shidu = (TextView) activity.findViewById(R.id.tv_shidu);
        tv_qiya = (TextView) activity.findViewById(R.id.tv_qiya);
        tv_pm25 = (TextView) activity.findViewById(R.id.tv_pm25);
        tv_zaoyin = (TextView) activity.findViewById(R.id.tv_zaoyin);
        tv_gas_one_name = (AlignTextView) activity.findViewById(R.id.tv_gas_one_name);
        tv_gas_one_value = (TextView) activity.findViewById(R.id.tv_gas_one_value);
        tv_gas_two_name = (AlignTextView) activity.findViewById(R.id.tv_gas_two_name);
        tv_gas_two_value = (TextView) activity.findViewById(R.id.tv_gas_two_value);
        tv_gas_three_name = (AlignTextView) activity.findViewById(R.id.tv_gas_three_name);
        tv_gas_three_value = (TextView) activity.findViewById(R.id.tv_gas_three_value);
        tv_gas_four_name = (AlignTextView) activity.findViewById(R.id.tv_gas_four_name);
        tv_gas_four_value = (TextView) activity.findViewById(R.id.tv_gas_four_value);
        tv_gas_five_name = (AlignTextView) activity.findViewById(R.id.tv_gas_five_name);
        tv_gas_five_value = (TextView) activity.findViewById(R.id.tv_gas_five_value);
        tv_gas_six_name = (AlignTextView) activity.findViewById(R.id.tv_gas_six_name);
        tv_gas_six_value = (TextView) activity.findViewById(R.id.tv_gas_six_value);
        tv_gas_seven_name = (AlignTextView) activity.findViewById(R.id.tv_gas_seven_name);
        tv_gas_seven_value = (TextView) activity.findViewById(R.id.tv_gas_seven_value);
        tv_device_id = (TextView)activity.findViewById(R.id.tv_device_id);
        cv_device_info.setOnClickListener(onClickListener);
        iv_wind_pointer = (ImageView) activity.findViewById(R.id.iv_wind_pointer);
        lv_history = (ListView) activity.findViewById(R.id.lv_history);
        historyInfoAdapter = new HistoryInfoAdapter(null, activity);
        lv_history.setAdapter(historyInfoAdapter);

//        筛选条件
        rb_gas_type = (RadioButton) activity.findViewById(R.id.rb_gas_type);
        rb_device_number = (RadioButton) activity.findViewById(R.id.rb_device_number);
        tv_filter_line = (TextView) activity.findViewById(R.id.tv_filter_line);
        lv_filter_device_id = (ListView) activity.findViewById(R.id.lv_filter_device_id);
        filterDeviceIdAdapter = new FilterDeviceIdAdapter(activity);
        lv_filter_device_id.setAdapter(filterDeviceIdAdapter);
        lv_filter_device_id.setOnItemClickListener(this);
        lv_filter_gas_type = (ListView) activity.findViewById(R.id.lv_filter_gas_type);
        filterGasTypeAdapter = new FilterGasTypeAdapter(activity, null);
        lv_filter_gas_type.setAdapter(filterGasTypeAdapter);
        lv_filter_gas_type.setOnItemClickListener(this);
        tl_history_top = (TableLayout) activity.findViewById(R.id.tl_history_top);
    }

    /**
     * 显示设备数据
     *
     * @param gasDevicesBean
     */
    public void showDeviceInfo(DeviceRealTimeInfo.GasDevicesBean gasDevicesBean) {
        List<DeviceRealTimeInfo.GasDevicesBean.GasInfosBean> gasInfosBeanList = gasDevicesBean.getGasInfos();
        if (gasInfosBeanList != null && gasInfosBeanList.size() > 0) {
            DeviceRealTimeInfo.GasDevicesBean.GasInfosBean gasInfosBean = null;
            tv_device_id.setText(gasDevicesBean.getDeviceId()+"");
            for (int i = 0; i < gasInfosBeanList.size(); i++) {
                gasInfosBean = gasInfosBeanList.get(i);
                switch (i) {
                    case 0:
                        tv_gas_one_name.setText(getGasName(gasInfosBean.getGasName()));
                        tv_gas_one_value.setText(String.valueOf(gasInfosBean.getGasVlaue()));
                        break;
                    case 1:
                        tv_gas_two_name.setText(getGasName(gasInfosBean.getGasName()));
                        tv_gas_two_value.setText(String.valueOf(gasInfosBean.getGasVlaue()));
                        break;
                    case 2:
                        tv_gas_three_name.setText(getGasName(gasInfosBean.getGasName()));
                        tv_gas_three_value.setText(String.valueOf(gasInfosBean.getGasVlaue()));
                        break;
                    case 3:
                        tv_gas_four_name.setText(getGasName(gasInfosBean.getGasName()));
                        tv_gas_four_value.setText(String.valueOf(gasInfosBean.getGasVlaue()));
                        break;
                    case 4:
                        tv_gas_five_name.setText(getGasName(gasInfosBean.getGasName()));
                        tv_gas_five_value.setText(String.valueOf(gasInfosBean.getGasVlaue()));
                        break;
                    case 5:
                        tv_gas_six_name.setText(getGasName(gasInfosBean.getGasName()));
                        tv_gas_six_value.setText(String.valueOf(gasInfosBean.getGasVlaue()));
                        break;
                    case 6:
                        tv_gas_seven_name.setText(getGasName(gasInfosBean.getGasName()));
                        tv_gas_seven_value.setText(String.valueOf(gasInfosBean.getGasVlaue()));
                        break;
                }
            }
        }
    }

    private String getGasName(String gasName) {
        String newGasName = "——";
        newGasName = TextUtils.isEmpty(gasName)?"——":gasName;
        newGasName = newGasName +":";
        return newGasName;
    }

    public void showAlarmInfo(DeviceAlarmInfo deviceAlarmInfo){
        tv_gas_one_name.setText(deviceAlarmInfo.getDeviceName());
//        tv_gas_one_value.setText(String.valueOf(deviceAlarmInfo.getGasType()));
    }

    public void showClimateInfo(DeviceRealTimeInfo.WeatherDeviceBean weatherDeviceBean) {
        tv_fengxiang.setText(weatherDeviceBean.getWindDirection() + "");
        tv_fengsu.setText(weatherDeviceBean.getWindSpeed() + "");
        tv_wendu.setText(weatherDeviceBean.getTemperature() + "");
        tv_shidu.setText(weatherDeviceBean.getHumidity() + "");
        tv_qiya.setText(weatherDeviceBean.getPressure() + "");
        tv_pm25.setText(weatherDeviceBean.getPm2_5() + "");
        tv_zaoyin.setText(weatherDeviceBean.getNoise() + "");
        rotateWindPointer(weatherDeviceBean.getWindDirection());
    }

    /**
     * 清除设备布局
     */
    public void clearDeviceInfo() {
//        tv_fengxiang.setText("");
//        tv_fengsu.setText("");
//        tv_wendu.setText("");
//        tv_shidu.setText("");
//        tv_qiya.setText("");
//        tv_pm25.setText("");
//        tv_zaoyin.setText("");
        tv_gas_one_name.setText("");
        tv_gas_one_value.setText("");
        tv_gas_two_name.setText("");
        tv_gas_two_value.setText("");
        tv_gas_three_name.setText("");
        tv_gas_three_value.setText("");
        tv_gas_four_name.setText("");
        tv_gas_four_value.setText("");
        tv_gas_five_name.setText("");
        tv_gas_five_value.setText("");
        tv_gas_six_name.setText("");
        tv_gas_six_value.setText("");
        tv_gas_seven_name.setText("");
        tv_gas_seven_value.setText("");
    }

    /**
     * 显示设备数据布局
     */
    public void showDeviceView() {
        cv_device_info.setVisibility(View.VISIBLE);

    }

    /**
     * 隐藏设备布局
     */
    public void hideDeviceView() {
        if (cv_device_info.getVisibility() == View.VISIBLE) {
            cv_device_info.setVisibility(View.GONE);
        }
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            hideDeviceView();
        }
    };

    /**
     *风向标设定：
     * @param degree
     */
    private void rotateWindPointer(float degree) {
        RotateAnimation ra = new RotateAnimation(currentDegree, degree,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        ra.setFillAfter(true);
        //旋转过程持续时间
        ra.setDuration(200);
        //罗盘图片使用旋转动画
        iv_wind_pointer.startAnimation(ra);
        currentDegree = degree;
    }

    public void setHistory(List<DeviceRealTimeInfo> gasHistoryInfos) {
        if (gasHistoryInfos != null && gasHistoryInfos.size() > 0) {
            for (int i = 0; i < gasHistoryInfos.size(); i++) {
                List<DeviceRealTimeInfo.GasDevicesBean> devicesBeanList = gasHistoryInfos.get(i).getGasDevices();
                if (devicesBeanList != null && devicesBeanList.size() > 0) {
                    historyInfoAdapter.addRealTimeDiviceData(devicesBeanList);
                }else{
                    historyInfoAdapter.notifyDataSetChanged();
                }
            }
        }else{
            historyInfoAdapter.notifyDataSetChanged();
        }
    }

    public void setGasType(List<GasType> gasTypeList) {
        filterGasTypeAdapter.setGasTypeList(gasTypeList);
    }

    public void showException(Exception e) {
        final ExceptionHintDialog dialog = new ExceptionHintDialog(activity);
        dialog.setDialogMessage(e.getMessage() + "");
        dialog.setOnOkClickListener(new ExceptionHintDialog.OnOkClickListener() {
            @Override
            public void onOkClick() {
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    public void showExcepitionMsg(String errMsg){
        final ExceptionHintDialog dialog = new ExceptionHintDialog(activity);
        dialog.setDialogMessage(errMsg);
        dialog.setOnOkClickListener(new ExceptionHintDialog.OnOkClickListener() {
            @Override
            public void onOkClick() {
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    public void showProgress() {
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
        progressDialog = new ProgressDialog(activity);
        progressDialog.setMessage(activity.getString(R.string.history_data_loading));
        progressDialog.show();
    }

    public void hideProgress() {
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
    }

    public void hideFilterView() {
        tv_filter_line.setVisibility(View.GONE);
        lv_filter_device_id.setVisibility(View.GONE);
        lv_filter_gas_type.setVisibility(View.GONE);
    }

    public void showFilterDeviceIdView() {
        tv_filter_line.setVisibility(View.VISIBLE);
        lv_filter_device_id.setVisibility(View.VISIBLE);
        lv_filter_gas_type.setVisibility(View.GONE);
    }

    public void showFilterGasTypeView() {
        tv_filter_line.setVisibility(View.VISIBLE);
        lv_filter_gas_type.setVisibility(View.VISIBLE);
        lv_filter_device_id.setVisibility(View.GONE);
    }

    public void hideHistoryView() {
        tl_history_top.setVisibility(View.GONE);
        lv_history.setVisibility(View.GONE);
    }

    public void showHistoryView() {
        tl_history_top.setVisibility(View.VISIBLE);
        lv_history.setVisibility(View.VISIBLE);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (parent.getId()) {
            case R.id.lv_filter_device_id:
                filterDeviceIdAdapter.setSelectedPosition(position);
                setSelectedDeviceName((String) filterDeviceIdAdapter.getItem(position));
                break;
            case R.id.lv_filter_gas_type:
                filterGasTypeAdapter.setSelectedPosition(position);
                GasType gasType = (GasType) filterGasTypeAdapter.getItem(position);
                setSelectedGasName(gasType.getGasName());
                break;
        }
    }

    public String getSelectedDeviceId() {
        selectedDeviceId = filterDeviceIdAdapter.getSelectedDeviceIdId();
        return selectedDeviceId;
    }

    public int getSelectedGasType() {
        selectedGasType = filterGasTypeAdapter.getSelectedGasType();
        return selectedGasType;
    }

    public void addRealTimeData(DeviceRealTimeInfo deviceRealTimeInfo) {
        if (deviceRealTimeInfo != null) {
            List<DeviceRealTimeInfo.GasDevicesBean> gasInfosBeanList = deviceRealTimeInfo.getGasDevices();
            if (gasInfosBeanList != null && gasInfosBeanList.size() > 0) {
                switch (selectDeviceNumber) {
                    case -1:
                        if (selectGasType == -1) {
                            //不筛选
                        } else {
                            //通过GasType筛选
                            gasInfosBeanList = queryDeviceGasInfosByGasType(deviceRealTimeInfo, selectGasType).getGasDevices();
                        }
                        break;
                    default:
                        if (selectGasType == -1) {
                            //通过设备id筛选
                            gasInfosBeanList = queryDeviceGasInfosByDeviceId(deviceRealTimeInfo,selectDeviceNumber).getGasDevices();
                        } else {
                            //通过设备id和GasType筛选
                            gasInfosBeanList = queryDeviceGasInfosByDeviceId(
                                    queryDeviceGasInfosByGasType(deviceRealTimeInfo,selectGasType),selectDeviceNumber).getGasDevices();
                        }
                        break;
                }
                historyInfoAdapter.addRealTimeDiviceData(gasInfosBeanList);
            }
        }
    }

    /**
     * 通过气体类型查询信息
     *
     * @param gasType
     * @return
     */
    private DeviceRealTimeInfo queryDeviceGasInfosByGasType(DeviceRealTimeInfo realTimeInfo, int gasType) {
        List<DeviceRealTimeInfo.GasDevicesBean> gasDevicesList = null;
        List<DeviceRealTimeInfo.GasDevicesBean.GasInfosBean> gasInfoList = null;
        DeviceRealTimeInfo deviceRealTimeInfo = new DeviceRealTimeInfo();
        if (realTimeInfo != null) {
            gasDevicesList = new ArrayList<>();
            gasInfoList = new ArrayList<>();
            for (int j = 0; j < realTimeInfo.getGasDevices().size(); j++) {
                DeviceRealTimeInfo.GasDevicesBean gasDevicesBean = realTimeInfo.getGasDevices().get(j);
                for (int k = 0; k < gasDevicesBean.getGasInfos().size(); k++) {
                    DeviceRealTimeInfo.GasDevicesBean.GasInfosBean gasInfosBean = gasDevicesBean.getGasInfos().get(k);
                    if (gasInfosBean.getGasType() == gasType) {
                        gasInfoList.add(gasInfosBean);
                        continue;
                    }
                }
                if (gasInfoList.size()==0)continue;
                DeviceRealTimeInfo.GasDevicesBean gasDevice = new DeviceRealTimeInfo.GasDevicesBean();
                gasDevice.setDeviceId(gasDevicesBean.getDeviceId());
                gasDevice.setDeviceLat(gasDevicesBean.getDeviceLat());
                gasDevice.setDeviceLng(gasDevicesBean.getDeviceLng());
                gasDevice.setGasInfos(gasInfoList);
                gasDevice.setGasTime(gasDevicesBean.getGasTime());
                gasDevicesList.add(gasDevice);
            }
            deviceRealTimeInfo.setDeviceSerial(realTimeInfo.getDeviceSerial());
            deviceRealTimeInfo.setWeatherDevice(realTimeInfo.getWeatherDevice());
            deviceRealTimeInfo.setGasDevices(gasDevicesList);
        }
        return deviceRealTimeInfo;
    }

    private DeviceRealTimeInfo queryDeviceGasInfosByDeviceId(DeviceRealTimeInfo realTimeInfo,int deviceId) {
        List<DeviceRealTimeInfo.GasDevicesBean> gasDevicesList = null;
        DeviceRealTimeInfo deviceRealTimeInfo = new DeviceRealTimeInfo();
        if (realTimeInfo!=null) {
            gasDevicesList = new ArrayList<>();
                for (int j = 0; j < realTimeInfo.getGasDevices().size(); j++) {
                    DeviceRealTimeInfo.GasDevicesBean gasDevicesBean = realTimeInfo.getGasDevices().get(j);
                    if (gasDevicesBean.getDeviceId() == deviceId) {
                        gasDevicesList.add(gasDevicesBean);
                    }
                }
                deviceRealTimeInfo.setGasDevices(gasDevicesList);
                deviceRealTimeInfo.setDeviceSerial(realTimeInfo.getDeviceSerial());
                deviceRealTimeInfo.setWeatherDevice(realTimeInfo.getWeatherDevice());
        }
        return deviceRealTimeInfo;
    }

    public void setDeviceId(int deviceId) {
        selectDeviceNumber = deviceId;
        historyInfoAdapter.setDeviceId(deviceId);
    }

    public void setGasTypeIndex(int gasTypeIndex) {
        selectGasType = gasTypeIndex;
        historyInfoAdapter.setGasType(gasTypeIndex);
    }

    public void clearHistoryData() {
        historyInfoAdapter.clearHistory();
    }

    private void setSelectedDeviceName(String deviceName) {
        rb_device_number.setText(deviceName);
    }

    private void setSelectedGasName(String gasName) {
        rb_gas_type.setText(gasName);
    }


}
